package com.mobile.collect.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mobile.collect.R;
import com.mobile.collect.beans.TypeInfo;
import com.mobile.collect.db.EntityManager;

import java.util.List;

import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListViewItemClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;

/**
 * 类名: KeyBroadPop
 * 作者: 李学伟
 * 日期: 2020年09月22日 16:39
 * 描述: dialog_keybroad
 */
public class TypeDialog extends InputDialog {

    RecyclerView numberView;
    BaseRecyclerAdapter<TypeInfo> numberAdapter;
    TypeInfo selectInfo;
    TypeCompleteListener typeCompleteListener;
    public TypeDialog(@NonNull Context context, String label, TypeCompleteListener listener) {
        super(context, label, listener, R.layout.dialog_number, 0);
        this.typeCompleteListener = listener;
    }

    @Override
    protected void initViews() {
        super.initViews();
        leftBtn.setVisibility(View.GONE);
        rightBtn.setVisibility(View.GONE);
        inputEt.setVisibility(View.GONE);
        numberView = findViewById(R.id.numberView);
        numberView.setLayoutManager(new GridLayoutManager(context, 3));
        numberAdapter = new BaseRecyclerAdapter<TypeInfo>(context) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_latters;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                holder.setText(R.id.valueTv, getItemByPosition(position).getChildName());
            }
        };
        numberView.setAdapter(numberAdapter);
        numberAdapter.addList(true, getTypes());

        numberAdapter.setItemClickListener(new ListViewItemClickListener() {
            @Override
            public void itemClick(int position) {
                selectInfo = numberAdapter.getItemByPosition(position);
                typeCompleteListener.onInputComplete(selectInfo.getChildCode());
                typeCompleteListener.setType(selectInfo);
//                typeCompleteListener.setChidType(selectInfo.getChName());
                dismiss();
            }
        });
    }

    @Override
    protected void appendText(String value) {
        inputEt.setText(value);
    }

    private List<TypeInfo> getTypes() {
        return EntityManager.getInstance().queryTypeInfos();
//        String chilType = FileUtil.getFromAssets(getContext(), "childtype.xml");
//        XMLUtil xmlUtil = new XMLUtil(chilType);
//        return xmlUtil.parserChildType();
    }

    public interface TypeCompleteListener extends InputCompleteListener{
        void setType(TypeInfo typeInfo);
    }

}
